package top.tangyh.lamp.care.common;

import cn.hutool.core.collection.CollectionUtil;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;
public class YclTreeUtil {


    /**
     * 获取层级的树结构函数
     *
     * @param list   数据列表
     * @param <node> treenode子类
     * @return 调整好的树结构
     */
    public static <node extends YcTreeNode> List<node> makeTreeList(List<node> list) {
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        } else if (list.size() == 1) {
            return list;
        }
        Map<Integer, node> nodeMap = list.stream().collect(Collectors.toMap(node::getDadId, node -> node));

        List<node> treeList = new ArrayList<>();
        for (node value : nodeMap.values()) {
            Integer parentId = value.getDadSupid();
            if (parentId == null || parentId == 0) {
                treeList.add(value);
            } else {
                node parentNode = nodeMap.get(parentId);
                if (parentNode != null) {
                    List childList = parentNode.getChildren();
                    if (childList == null) {
                        parentNode.setChildren(new ArrayList<>());
                    }
                    parentNode.getChildren().add(value);
                    if (parentNode.getChildren().size() > 1) {
                        parentNode.getChildren().sort(Comparator.comparing(node::getDadSort));
                    }
                } else {
                    treeList.add(value);
                }
            }
        }
        treeList.sort(Comparator.comparing(node::getDadSort));
        return treeList;
    }


    /**
     * 树形列表转为平级列表并清空各节点子集
     *
     * @param newList  传入新的空集合
     * @param treeList 树形列表数据
     * @param <node>
     * @return
     */
    public static <node extends YcTreeNode> List<node> treeToList(List<node> newList, List<node> treeList) {
        if (CollectionUtil.isNotEmpty(treeList)) {
            treeList.forEach(node -> {
                newList.add(node);
                if (CollectionUtil.isNotEmpty(node.getChildren())) {
                    treeToList(newList, node.getChildren());
                    node.setChildren(null);
                }
            });
        }
        return newList;
    }


    /**
     * 获取一个节点下的子节点列表 不包含chooseNode节点
     *
     * @param childList
     * @param allNodeList
     * @param chooseNodeId
     * @param <node>
     * @return
     */
    public static <node extends YcTreeNode> List<node> getChildNodeListByOneNode(List<node> childList, List<node> allNodeList, Integer chooseNodeId) {
        if (chooseNodeId == 0) {
            return allNodeList;
        }
        allNodeList.forEach(node -> {
            if (node.getDadSupid().equals(chooseNodeId)) {
                getChildNodeListByOneNode(childList, allNodeList, node.getDadId());
                childList.add(node);
            }
        });
        return childList;
    }

    /**
     * 获取一个节点下的子节点id列表 不包含chooseNode节点
     *
     * @param childList
     * @param allNodeList
     * @param chooseNodeId
     * @param <node>
     * @return
     */
    public static <node extends YcTreeNode> Set<Integer> getChildIdListByOneNode(Set<Integer> childList, List<node> allNodeList, Integer chooseNodeId) {
        allNodeList.forEach(node -> {
            if (node.getDadSupid().equals(chooseNodeId)) {
                getChildIdListByOneNode(childList, allNodeList, node.getDadId());
                childList.add(node.getDadId());
            }
        });
        return childList;
    }


}